package cn.hicard.risk.interf.limit;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import org.apache.log4j.Logger;
import org.codehaus.jackson.map.ObjectMapper;
import cn.hicard.risk.common.Constant;
import cn.hicard.risk.interf.tools.JsonPrintUtil;
import cn.hicard.risk.interf.tools.Post;
import cn.hicard.risk.interf.tools.UrlUtil;
import cn.hicard.risk.interf.vo.AppHead;
import cn.hicard.util.DateHelp;

/**
 * 商户清算资金限额与清算系统接口
 * 银行调单接口
 * @author shaoyan
 * 
 */
public class FreezeInfoInterf {

	/** 服务端接口处理成功 接口编号+ _00*/
	/** 服务端接口处理失败 接口编号+ _01*/

	private static Logger logger = Logger.getLogger(Constant.LOGGER_MAIN);

	/**
	 * 1、从清算系统获取商户的日清算总额等
	 * 2、向新清算发送银行调单信息
	 * @param vo
	 * @return
	 * @throws Exception
	 */
	public RiskLimitFreezeInterface receiveSumMoney(RiskLimitFreezeInterface vo,String transType)
			throws Exception {

		AppHead appHead = new AppHead();
		appHead.setTransType(transType);
		appHead.setSerialNo(DateHelp.dateNumber());
		appHead.setDataTime(DateHelp.dateTime());

		LimitFreezeInfo sendBean = new LimitFreezeInfo();
		sendBean.setAppHead(appHead);
		sendBean.setAppbody(vo);

		RetBean retBean = null;
		try {

			retBean = assemblingMessage(sendBean, "receiveSumMoney");
			String retCode = transType+"_00";
			if (!retCode.equals(retBean.getRetCode())) {
				logger.error("风控与清算系统接口[receiveSumMoney]失败，原因:"
						+ retBean.getRetMessage());
				throw new Exception();
			}

		} catch (Exception e) {

			logger.error("风控与清算系统接口[receiveSumMoney]失败");
			throw new Exception(e.getMessage());
		}

		return retBean.getAppbody();
	}

	/**
	 * 将商户的日冻结解冻信息发送给清算系统
	 * 
	 * @param vo
	 * @return
	 * @throws Exception
	 */
	public boolean sendFreezeInfo(RiskLimitFreezeInterface vo,String transType) throws Exception {
		boolean flag = false;

		AppHead appHead = new AppHead();
		appHead.setTransType(transType);
		appHead.setSerialNo(DateHelp.dateNumber());
		appHead.setDataTime(DateHelp.dateTime());

		LimitFreezeInfo sendBean = new LimitFreezeInfo();
		sendBean.setAppHead(appHead);
		sendBean.setAppbody(vo);

		try {
			RetBean retBean = assemblingMessage(sendBean, "sendFreezeInfo");
			String retCode = transType+"_00";
			if (retCode.equals(retBean.getRetCode())) {
				flag = true;

			} else {
				logger.error("风控与清算系统接口[sendFreezeInfo]失败，原因:"
						+ retBean.getRetMessage());
				throw new Exception();
			}
		} catch (Exception e) {

			logger.error("风控与清算系统接口[sendFreezeInfo]失败");
			throw new Exception(e.getMessage());
		}

		return flag;
	}

	/**
	 * 客户端发起json数据请求
	 * 
	 * @param vo
	 * @return
	 * @throws Exception
	 * @throws UnsupportedEncodingException
	 * @throws IOException
	 */
	public RetBean assemblingMessage(LimitFreezeInfo vo, String funName)
			throws Exception {
		String sJson = ""; // 发送数据
		String backStr = ""; // 接收清算返回数据
		RetBean retBean = null;

		UrlUtil urlutil = new UrlUtil();
		String url = urlutil.getUrl();// 获取清算接口地址

		try {

			JsonPrintUtil json = new JsonPrintUtil();// 转换为JSON格式
			sJson = json.objectToJson(vo);
			logger.info("调用清算接口" + funName + "，风控原始请求数据为：[" + sJson + "]");
			Post hPost = new Post(url, sJson);
			backStr = new String(hPost.postjson(url, sJson).getBytes("gbk"));
			// hPost.start();// 启动线程
			logger.info("清算返回的json为：[" + backStr + "]");

			ObjectMapper mapper = new ObjectMapper();
			retBean = mapper.readValue(backStr, RetBean.class);

		} catch (Exception e) {

			logger.error("风控与清算系统接口[assemblingMessage]发生异常", e);
			throw new Exception(e.getMessage());
		}
		return retBean;
	}

	public static void main(String[] args) {

		FreezeInfoInterf interf = new FreezeInfoInterf();
		RiskLimitFreezeInterface vo = new RiskLimitFreezeInterface();
		try {
			//vo.setFreezeId("01588072300169020131119090000");//唯一标识
			vo.setFreezeId("19440154990010220131119160001");//唯一标识
			vo.setHmId("194401549900102");//商户号
			//vo.setHmId("194401566100023");//商户号
			vo.setMerchName("测试商户二");
			vo.setFreezeMoney(50000);//应冻结金额
			vo.setFreezeTime("2013-11-19 09:00:00");
			vo.setStatus("6");
			
			interf.receiveSumMoney(vo, "F1004");
			
			
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

}
